//
//  YZSettingHelpTableViewController.m
//  kuaiyidemo1
//
//  Created by 刘邓诏初 on 16/5/5.
//  Copyright © 2016年 刘邓诏初. All rights reserved.
//

#import "YZSettingHelpTableViewController.h"
#import "YZSettingHelpMenuModel.h"
#import "YZInstructionsViewController.h"
#import "YZChangePasswordViewController.h"

@interface YZSettingHelpTableViewController ()
/// 设置帮助菜单cell对应的数组
@property(nonatomic,strong)NSArray *settingHelpMenuArr;

@end

@implementation YZSettingHelpTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建菜单列表数组
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - 数据源和代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.settingHelpMenuArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"YZSettingHelpTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 获取模型
    YZSettingHelpMenuModel *settingHelpMenuModel = self.settingHelpMenuArr[indexPath.row];
    cell.textLabel.text = settingHelpMenuModel.title;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // 取消点击效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定要退出吗？" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            // TODO: 注销账号
            // ake 2016.05.11添加
            [self.navigationController popToRootViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"logOut" object:nil];
        }];
        [alert addAction:cancel];
        [alert addAction:destructive];
        [self presentViewController:alert animated:YES completion:nil];

    }
    else {
        // 获取模型
        YZSettingHelpMenuModel *settingHelpMenuModel = self.settingHelpMenuArr[indexPath.row];
        if (settingHelpMenuModel.destVc == nil) {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Suggest" bundle:nil];
            UIViewController * Vc = sb.instantiateInitialViewController;
            Vc.title = settingHelpMenuModel.title;
            [self.navigationController pushViewController:Vc animated:YES];

        }else{
        UIViewController *destVc = [[settingHelpMenuModel.destVc alloc] init];
        destVc.title = settingHelpMenuModel.title;
        [self.navigationController pushViewController:destVc animated:YES];
        }
    }
}


#pragma mark - 懒加载
- (NSArray *)settingHelpMenuArr{
    if (!_settingHelpMenuArr) {
        // TODO: 意见反馈控制器
        YZSettingHelpMenuModel *feedback = [YZSettingHelpMenuModel settingHelpMenuModelWithTitle:@"意见反馈" destVc:nil];
        YZSettingHelpMenuModel *instructions = [YZSettingHelpMenuModel settingHelpMenuModelWithTitle:@"加号符号说明" destVc:[YZInstructionsViewController class]];
        YZSettingHelpMenuModel *changePassword = [YZSettingHelpMenuModel settingHelpMenuModelWithTitle:@"修改密码" destVc:[YZChangePasswordViewController class]];
        YZSettingHelpMenuModel *logout = [YZSettingHelpMenuModel settingHelpMenuModelWithTitle:@"注销用户" destVc:nil];
        
        _settingHelpMenuArr = [NSArray arrayWithObjects:feedback, instructions, changePassword, logout, nil];
    }
    return _settingHelpMenuArr;
}

@end
